package com.yfjin.webrtc;

import android.graphics.ImageFormat;
import android.graphics.Rect;
import android.graphics.YuvImage;
import android.hardware.Camera;
import android.os.Environment;
import android.util.Log;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

public class Util {

    public static byte[] shun(Camera.Size size, byte[] src) {
        byte[] des = new byte[size.width * size.height * 3 / 2];
        int width = size.width;
        int height = size.height;
        int wh = width * height;
        //旋转Y
        int k = 0;
        for (int i = 0; i < width; i++) {
            for (int j = 0; j < height; j++) {
                des[k] = src[width * j + i];
                k++;
            }
        }
        for (int i = 0; i < width; i += 2) {
            for (int j = 0; j < height / 2; j++) {
                des[k] = src[wh + width * j + i];
                des[k + 1] = src[wh + width * j + i + 1];
                k += 2;
            }
        }
        return des;
    }

    public static byte[] ni(Camera.Size size, byte[] src) {
        byte[] des = new byte[size.width * size.height * 3 / 2];
        int width = size.width;
        int height = size.height;

        int nWidth = 0, nHeight = 0;
        int wh = 0;
        int uvHeight = 0;
        if (width != nWidth || height != nHeight) {
            nWidth = width;
            nHeight = height;
            wh = width * height;
            uvHeight = height >> 1;//uvHeight = height / 2
        }
        //旋转Y
        int k = 0;
        for (int i = 0; i < width; i++) {
            int nPos = width - 1;
            for (int j = 0; j < height; j++) {
                des[k] = src[nPos - i];
                k++;
                nPos += width;
            }
        }

        for (int i = 0; i < width; i += 2) {
            int nPos = wh + width - 1;
            for (int j = 0; j < uvHeight; j++) {
                des[k] = src[nPos - i - 1];
                des[k + 1] = src[nPos - i];
                k += 2;
                nPos += width;
            }
        }
        return des;
    }


    public static void captrue(Camera.Size size, byte[] temp) {

        String fileName = "IMG.jpg";  //jpeg文件名定义
        File sdRoot = Environment.getExternalStorageDirectory();    //系统路径

        File pictureFile = new File(sdRoot, fileName);
        if (pictureFile.exists()) {
            pictureFile.delete();
        }

        if (!pictureFile.exists()) {
            try {
                pictureFile.createNewFile();

                FileOutputStream filecon = new FileOutputStream(pictureFile);
                //ImageFormat.NV21 and ImageFormat.YUY2 for now
                YuvImage image = new YuvImage(temp, ImageFormat.NV21, size.height, size.width, null);   //将NV21 data保存成YuvImage
                //图像压缩
                image.compressToJpeg(new Rect(0, 0, image.getWidth(), image.getHeight()),
                        100, filecon);   // 将NV21格式图片，以质量70压缩成Jpeg，并得到JPEG数据流
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }


    public static byte[] nv21toNV12(byte[] nv21) {
        int size = nv21.length;
        byte[] nv12 = new byte[size];
        int len = size * 2 / 3;
        System.arraycopy(nv21, 0, nv12, 0, len);

        int i = len;
        while (i < size - 1) {
            nv12[i] = nv21[i + 1];
            nv12[i + 1] = nv21[i];
            i += 2;
        }
        return nv12;
    }

    public static byte[] nv21toNV12_2(byte[] nv21) {
        int size = nv21.length;
        byte[] nv12 = new byte[size];
        int len = size * 2 / 3;
        System.arraycopy(nv21, 0, nv12, 0, len);

        int i = len;
        Log.i("maniu", "i:" + i);
        while (i < size - 1) {
            nv12[i] = nv21[i + 2];
            nv12[i + 1] = nv21[i + 3];
            nv12[i + 2] = nv21[i];
            nv12[i + 3] = nv21[i + 1];
            i += 4;
        }
//        while (i < size - 1) {
//            nv12[i] = nv21[i];
//            i++;
//        }


        return nv12;
    }


    public static byte[] NV21ToNV12(byte[] nv21, int width, int height) {

        byte[] nv12 = new byte[nv21.length];

        int framesize = width * height;
        int i = 0, j = 0;
        System.arraycopy(nv21, 0, nv12, 0, framesize);
        for (i = 0; i < framesize; i++) {
            nv12[i] = nv21[i];
        }
        for (j = 0; j < framesize / 2; j += 2) {
            nv12[framesize + j - 1] = nv21[j + framesize];
        }
        for (j = 0; j < framesize / 2; j += 2) {
            nv12[framesize + j] = nv21[j + framesize - 1];
        }

        return nv12;
    }
}
